package com.jdbcdemo.test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class DaoOperat {
	Connection conn = null;

	public void insert(Timestamp time) throws SQLException {
		String sql = "insert into time(time) values(?)";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setTimestamp(1, time);
		pst.executeUpdate();
		conn.commit();
	}
	public void insert2(String time) throws SQLException {
		String sql = "insert into time(time) values(?)";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1,  time);
		pst.executeUpdate();
		conn.commit();
	}
	public void select() throws SQLException {
		String sql = "select time from time";
		PreparedStatement pst = conn.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			System.out.println("时间=" + rs.getString("time"));
		}
		conn.commit();
		pst.close();
		conn.close();
	}

	public void empty() throws SQLException {
		String sql = "truncate table time";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.executeUpdate();
	}

	public DaoOperat() throws IOException, ClassNotFoundException, SQLException {
		initSql();
	}

	public void initSql() throws IOException, ClassNotFoundException,
			SQLException {
		DBconnect dbconn = new DBconnect();
		if (conn == null)
			conn = dbconn.getConn();
	}
}
